package com.zygame.sdk.view.dialog;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.TextView;

import com.zygame.sdk.utils.res.ResourceUtil;

public class SaveAccountView{

	private Context mContext ;
	
	private TextView fy_tv_account;
	private TextView fy_tv_pwd;
	private ImageView fy_logo;
	
	private View view;
	
	private String mUserName;
	private String mPassword;
	
	private Map<String,String> map;
	
	public SaveAccountView(Context context) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
	}
	
	public SaveAccountView(Context context,Map<String,String> map) {
		this.mContext = context;
		this.map = map;
		
		initScreenView();
	}
	
	private void initScreenView() {

		LayoutInflater inflater = LayoutInflater.from(mContext);
		view = inflater.inflate(ResourceUtil.getLayoutId(mContext, "zy_ly_view_save_account_picture"), null);
		fy_tv_account = (TextView) view.findViewById(ResourceUtil.getId(mContext, "fy_tv_account"));
		fy_tv_pwd = (TextView) view.findViewById(ResourceUtil.getId(mContext, "fy_tv_pwd"));
		fy_logo = (ImageView) view.findViewById(ResourceUtil.getId(mContext, "fy_logo"));
		
		fy_tv_account.setTextColor(Color.BLACK);
		fy_tv_pwd.setTextColor(Color.BLACK);
		
//		Drawable d1 = mContext.getResources().getDrawable(ResourceUtil.getDrawableId(mContext, "fy_user"));
//		d1.setBounds(5, 5, 80, 80);
//		fy_tv_account.setCompoundDrawables(d1, null, null, null);
//
//		Drawable d2 = mContext.getResources().getDrawable(ResourceUtil.getDrawableId(mContext, "fy_pwd"));
//		d2.setBounds(2, 2, 80, 80);
//		fy_tv_pwd.setCompoundDrawables(d2, null, null, null);
		
		mUserName = map.get("mUserName");
		mPassword = map.get("mPassword");
		fy_tv_account.setText("  " + mUserName);
		fy_tv_pwd.setText("  " + mPassword);
		
		screenshot();
	}
	
	private void screenshot(){
		Bitmap bitmap = convertViewToBitmap(view);
		String picPath = null;



//		try {
//			picPath = MediaStore.Images.Media.insertImage(mContext.getContentResolver(), bitmap, mUserName + ".jpg", null);
//			picPath = uripathToAbsPath(picPath);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		new SingleMediaScanner(mContext, picPath);  //刷新
//		 new SaveAccountDialog(mContext, 200, 100).show();



		FileOutputStream fos;
		try {
			// 判断手机设备是否有SD卡
			boolean isHasSDCard = Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED);
			if (isHasSDCard) {

//				 picPath =  Environment.getExternalStorageDirectory().getPath()  + "/zysdk";
				 picPath =  Environment.getExternalStorageDirectory()  + "/zysdk";
				File file1 = new File(picPath);
				File file = new File(file1, mUserName + ".png");
				if (!file1.exists()) {
					file1.mkdirs();
				}


				fos = new FileOutputStream(file);
			} else
				throw new Exception("创建文件失败!");

			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

			fos.flush();
			fos.close();
			new SingleMediaScanner(mContext, picPath);  //刷新
		} catch (Exception e) {
			e.printStackTrace();
		}


		/**
		 * 此方法在4.4系统不可用
		 * Android4.4中限制了系统应用才有权限使用广播通知系统扫描SD卡.
		 * mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
		 */
	}
	
	/**
	 * 转换uri路径
	 * @param uriPath
	 * @return
	 */
	public String uripathToAbsPath(String uriPath) {
		Uri uri = Uri.parse(uriPath);
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor actualimagecursor = ((Activity)mContext).managedQuery(uri, proj, null, null, null);
		int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		actualimagecursor.moveToFirst();
		String img_path = actualimagecursor.getString(actual_image_column_index);
		
		return img_path;
	}
	
	/**
	 * view转换成bitmap
	 * 
	 * @param view
	 * @return
	 */
	public static Bitmap convertViewToBitmap(View view) {
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();

		return bitmap;
	}
	
	public class SingleMediaScanner implements MediaScannerConnectionClient {

		private MediaScannerConnection mMs;
		private String mPath;

		public SingleMediaScanner(Context context, String path) {
			this.mPath = path;
			mMs = new MediaScannerConnection(context, this);
			mMs.connect();
		}

		@Override
		public void onMediaScannerConnected() {
			mMs.scanFile(mPath, null);
		}

		@Override
		public void onScanCompleted(String path, Uri uri) {
			mMs.disconnect();
		}
	}









}
